﻿// ------------------------------------------------------------------------------------
//      Copyright (c) 2012 uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 12/24/2012 7:13:16 PM
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Service.Services.Test
{
    using System;
    using System.Linq;
    using CP.NLayer.Models.Entities;
    using CP.NLayer.Service.Contracts;
    using Microsoft.Practices.Unity;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class CrudTest
    {
        [TestMethod]
        public void InsertTest()
        {
            var service = DependencyInjection.Container.Resolve<IRoleService>();
            var oldValue = service.GetAll().Count();
            var role = new Role { Name = Guid.NewGuid().ToString() };
            service.Insert(role);
            var newValue = service.GetAll().Count();
            Assert.AreEqual<int>(oldValue + 1, newValue);
        }

        [TestMethod]
        public void UpdateTest()
        {
            string oldValue;
            string newValue;
            string id;
            var service = DependencyInjection.Container.Resolve<IRoleService>();
            var role = service.GetAll().FirstOrDefault();
            id = role.Id;
            oldValue = role.Name;
            do
            {
                newValue = Guid.NewGuid().ToString();
            }
            while (newValue == oldValue);
            role.Name = newValue;
            service.Update(role);

            var service2 = DependencyInjection.Container.Resolve<IRoleService>();
            var role2 = service2.GetById(id);
            Assert.AreNotEqual(oldValue, role2.Name);
            Assert.AreEqual(newValue, role2.Name);
        }

        [TestMethod]
        public void DeleteTest()
        {
            var service = DependencyInjection.Container.Resolve<IRoleService>();
            var oldValue = service.GetAll().Count();
            var role = service.GetAll().FirstOrDefault();
            service.Delete(role);
            var newValue = service.GetAll().Count();
            Assert.AreEqual<int>(oldValue - 1, newValue);
        }
    }
}
